package com.example.demo.config.shiro.filter;

import com.example.demo.config.shiro.CookieUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.servlet.AdviceFilter;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class CookieFilter extends AdviceFilter {

    private final static String REDIRECT_URI_KEY = "redirect_uri";

    @Autowired
    private Cookie cookieTemplate;

    @Override
    protected void postHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        resp.setHeader("Access-Control-Allow-Credentials", "true");

        printCookie(req, resp);

        final String requestUri = WebUtils.getRequestUri(req);

        SavedRequest savedRequest = new SavedRequest(req);
        String queryString = savedRequest.getQueryString();

        Map<String, String> queryMap = getParams(queryString);

        svaeParam(req, resp, queryMap);
//        CookieUtil.setSessionIdCookie();

        System.out.println("----");
    }


    public void afterCompletion(ServletRequest request, ServletResponse response, Exception exception) throws Exception {
        if () {

        }
    }




















    private void printCookie(HttpServletRequest req, HttpServletResponse res) {
        log.info("redirect_uri:{}.", WebUtils.getRequestUri(req));

        javax.servlet.http.Cookie[] cookies = req.getCookies();
        if (ArrayUtils.isEmpty(cookies)) {
            return;
        }
        for (javax.servlet.http.Cookie c : cookies) {
            log.info("cookie[]name:{}, value:{}.", c.getName(), c.getValue());
        }

        log.info("");
        log.info("");
    }

    private void svaeParam(HttpServletRequest req, HttpServletResponse resp, Map<String, String> queryMap) {
        if (CollectionUtils.isEmpty(queryMap)) {
            return;
        }
        saveCookie(req, resp, REDIRECT_URI_KEY, queryMap.get(REDIRECT_URI_KEY));

        saveCookie(req, resp, "AA", "_AA_");

    }

    private void saveCookie(HttpServletRequest req, HttpServletResponse resp, String name, String value) {
        if (StringUtils.isNoneEmpty(value)) {
            SimpleCookie cookie = new SimpleCookie(cookieTemplate);
            cookie.setName(name);
            cookie.setValue(value);
            cookie.setDomain("127.0.0.1:8080");
            cookie.setPath("/");

            cookie.saveTo(req, resp);
        }
    }

    private Map<String, String> getParams(String queryString) {
        String[] splits = StringUtils.split(queryString, "&");
        if (ArrayUtils.isEmpty(splits)) {
            return Collections.emptyMap();
        }
        Map<String, String> map = new HashMap<>(splits.length);

        for (int i = 0; i < splits.length; i ++) {
            String[] strs = StringUtils.split(splits[i], "=");
            if (ArrayUtils.isEmpty(strs) || strs.length < 2) {
                continue;
            }
            map.put(strs[0], strs[1]);
        }

        return map;
    }
}
